home *** CD-ROM | disk | FTP | other *** search
- Path: rain.fr!world-net!usenet
- From: Frederic LACHASSE <lachass@worldnet.fr>
- Newsgroups: comp.lang.c++
- Subject: Re: cout << tab(5) ... without OMANIP(int)
- Date: Sat, 09 Mar 1996 21:15:53 +0000
- Organization: World-Net information exchange, Internet provider.
- Message-ID: <VA.00000064.0003e721@fred>
- References: <Bentley_Joe-0503961032270001@129.197.157.33>
- Reply-To: lachass@worldnet.fr
- NNTP-Posting-Host: client79.sct.fr
- X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
-
- In article <Bentley_Joe-0503961032270001@129.197.157.33>,
- Bentley_Joe@mm.rdd.lmsc.lockheed.com (Joe Bentley) wrote:
- >
- > I would like to write a tab function/manipulator that I can use in an
- > output stream. I know how to do this using the OMANIP(int) macro
- > approach, but would like another solution. I assumed that I could do it
- > with a function template, but I'm not sure how to approach the problem.
- > Any ideas?
-
- manipulators with arguments are two phases process: first create a
- temporary object that will store the parameters then have an ostream&
- operator <<(ostream&, const object&) that will do the actual job.
-
- The most simple way for a stand-alone manipulator:
-
- class tab
- {
- friend ostream& operator <<(ostream&, const tab&);
- int arg;
- public:
- tab(int a) : arg(a) {}
- };
-
- ostream& operator <<(ostream& os, const tab& t)
- {
- // t.arg contain the argument
- // job here
- }
-
- so: "ostream << tab(5);" will create a temporary object of class "tab" and
- call the operator <<.
-
-
-
- A generic object can be used for all manipulator with an int parameter:
-
- class omanip_int
- {
- friend ostream& operator <<(ostream&, const omanip_int&);
- typedef ostream& (*pfnType)(ostream&, int);
- pfnType pfn;
- int arg;
- public:
- omanip_int(pfnType fn, int a) : pfn(fn), arg(a) {}
- };
-
- inline ostream& operator <<(ostream& os, const omanip_int& om)
- { return (*pfnType)(os, arg); }
-
- (Of course, this class can be easily templatized to handle any kind of
- argument)
-
- So manipulators can be defined more simply as:
-
- ostream& tab_exec(ostream& os, int arg)
- {
- // job to do
- return os;
- }
-
- inline omanip_int tab(int arg)
- { return omanip_int(&tab_exec, arg); }
-
-
- I hope this'll help.
-
- Frederic LACHASSE (ECP 86)
- CompuServe: 100530,2005
- Internet: lachass@worldnet.fr
-
-